package com.aquarium.ui.controller;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.ui.controller.TimePickListViewAdapter.CheckBoxStatusChangedListener;

public abstract class ControllerSetting extends Activity {

	public final static String INTENT_DATA_TITLE = "title";

	public final static String ACTION_ADD_SETTING = "ACTION_ADD_SETTING";
	public final static String ACTION_MODIFY_SETTING = "ACTION_MODIFY_SETTING";
	public final static int REQUEST_ADD_SETTING = 1;
	public final static int REQUEST_MODIFY_SETTING = 2;
	public final static String INTENT_EXTRA_LISTVIEW_CLICKED_POSITION = "INTENT_EXTRA_LISTVIEW_CLICKED_POSITION";

	protected Button mAddButton;
	protected Button mDeleteButton;
	protected TextView mTitle;
	protected ListView mListView;
	protected ArrayList<Integer> mDeleteList;
	protected ModuleSettingDataBase mModuleSettingDataBase;
	protected TimePickListViewAdapter mTimePickListViewAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.controller_option_setting);
		mModuleSettingDataBase = ModuleSettingDataBase.getInstance(this);
		mDeleteList = new ArrayList<Integer>();
		subInit();
		findViews();
		createListViewAdapter();
		setListViewBinder();
		setListeners();
		mListView.setAdapter(mTimePickListViewAdapter);
	}

	protected void subInit(){
		
	}
	
	private void findViews() {
		mAddButton = (Button) findViewById(R.id.new_btn);
		mDeleteButton = (Button) findViewById(R.id.delete_btn);
		mTitle = (TextView) findViewById(R.id.time_pick_title_txv);
		mListView = (ListView) findViewById(R.id.cntler_setting_listview);
	}

	public boolean bindListView(View view, Cursor cursor) {
		int id = view.getId();
		switch (id) {
		case R.id.start_time_txv:
			int[] time = mModuleSettingDataBase.getTime(cursor);
			String timeStr = time[0] + ":" + time[1];
			((TextView) view).setText(timeStr);
			if (mModuleSettingDataBase.isAm(cursor)) {
				view.setBackgroundColor(0x00000000);
				((TextView) view).setTextColor(Color.BLACK);
			} else {
				view.setBackgroundColor(Color.BLACK);
				((TextView) view).setTextColor(Color.WHITE);
			}
			break;

		case R.id.mon_txv:
			if (mModuleSettingDataBase.isMonSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.tues_txv:
			if (mModuleSettingDataBase.isTuesSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.wed_txv:
			if (mModuleSettingDataBase.isWedSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.thur_txv:
			if (mModuleSettingDataBase.isThurSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.fri_txv:
			if (mModuleSettingDataBase.isFriSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.sat_txv:
			if (mModuleSettingDataBase.isSatSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;
		case R.id.sun_txv:
			if (mModuleSettingDataBase.isSunSet(cursor)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}
			break;

		default:
			return false;
		}
		return true;
	}

	public void setListeners() {
		mTimePickListViewAdapter
				.setCheckBoxStatusChangedListener(new CheckBoxStatusChangedListener() {

					@Override
					public void onChanged(int pos, boolean isChecked) {
						if (isChecked) {
							mDeleteList.add(pos);
						} else {
							mDeleteList.remove((Integer) pos);
						}
					}
				});
	}

	public abstract void setListViewBinder();

	public abstract void createListViewAdapter();

}
